package goodstemplatelogic

import (
	"common/dao/model"
	"context"
	"errors"
	"gorm.io/gorm"
	"time"

	"service/internal/svc"
	"service/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteLogic {
	return &DeleteLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DeleteLogic) Delete(in *pb.UuId) (*pb.Emp, error) {
	// todo: add your logic here and delete this line
	var (
		data = model.GoodsTemplate{}
	)
	db := l.svcCtx.DB.Table(model.TableNameGoodsTemplate)
	err := db.Transaction(func(tx *gorm.DB) error {
		err := tx.Where("uuid = ?", in.Uuid).Where("is_del=?", 0).Find(&data).Error
		if err != nil {
			return err
		}
		if data.UUID == "" {
			return errors.New("未查询到：" + in.Uuid)
		}
		t := time.Now()
		data.DeleTime = &t
		data.IsDel = 1
		err = tx.Save(&data).Error
		if err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	return &pb.Emp{}, nil
}
